package com.LeeCode;

/**
 * 找出所有子集的异或总和再求和
 */

public class Code1863 {
    public static void main(String[] args) {
        int[] nums = {1, 3};
        System.out.println(new Code1863().subsetXORSum(nums));
    }

    public int subsetXORSum(int[] nums) {
        int ans = 0, n = nums.length;

        for (int i = 0; i < 1 << n; i++) {
            int sum = 0;
            for (int j = 0; j < n; j++) {  // 判断第j位是否在子集中
                if ((i >> j & 1) == 1) {
                    sum = sum ^ nums[j];
                }
            }
            ans += sum;
        }
        return ans;
    }
}
